<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *活动管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	} 
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_weishop_cutprice_act',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_weishop_cutprice/detail//id='.$item['id'],true);
			$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']));
	
			$item['goods_title']=$goods['title'];
			$item['image']=$goods['image'];
			$item['starttime']=date('y-m-d h:i:s',$item['starttime']);
			$item['createtime']=date('y-m-d h:i:s',$item['createtime']);
			$item['endtime']=date('y-m-d h:i:s',$item['endtime']);
			$item['lidu']='每'.$item['cuttime'].'分钟降'.$item['cutprice'].'元';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'活动列表'),
			array('op'=>'edit','title'=>'添加活动'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'活动名称','type'=>'string'),
			array('name'=>'goods_title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('type'=>'img_string','title'=>'价格',
			'data'=>array(
				array('title'=>'原价：','name'=>'original'),
				array('title'=>'起拍价：','name'=>'startprice'),
				array('title'=>'最低价:','name'=>'stopprice'),
				)
			),
			array('title'=>'降价力度:','name'=>'lidu','type'=>'string'),
			array('name'=>'total','title'=>'库存','type'=>'string'),
			array('type'=>'img_string','title'=>'时间',
			'data'=>array(
				array('title'=>'拍卖开始时间：','name'=>'starttime'),
				array('title'=>'预计结束时间：','name'=>'endtime'),
				array('title'=>'创建活动时间：','name'=>'createtime')
				)
			),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
		//	array('name'=>'url','title'=>'复制链接','type'=>'copy'),
		//	array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'活动名称','type'=>'string','width'=>'15'),
			array('name'=>'status','title'=>'是否启用','type'=>'select','options'=>'status','width'=>'15'),
		)
	);
	$return['status']=array(array('id'=>'0','title'=>'关闭'),array('id'=>'1','title'=>'开启'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_cutprice_act',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['starttime']=$item['starttime']?date('Y-m-d H:i',$item['starttime']):'';
	$item['endtime']=$item['endtime']?date('Y-m-d H:i',$item['endtime']):'';
	$return['item']=$item;
	$return['item']['images']=json_decode($return['item']['images'],true);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回活动列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
							
								array('type'=>'string','title'=>'活动名称','name'=>'title','help'=>''),
								array('type'=>'string','title'=>'排序','name'=>'displayorder','help'=>'填写数字，按从小到大排序展示'),
								array('name'=>'goodsid','title'=>'选择商品','type'=>'choose_title','action'=>'get_goods','type'=>'choose_up'),
								array('type'=>'string','title'=>'商品库存','name'=>'total','help'=>''),
								array('type'=>'images','title'=>'商品图片','name'=>'images','help'=>''),
								array('type'=>'string','title'=>'商品原价','name'=>'original','help'=>''),
								array('type'=>'string','title'=>'起拍价','name'=>'startprice','help'=>''),
								array('type'=>'string','title'=>'最低价','name'=>'stopprice','help'=>''),
								array('title'=>'降价力度','name1'=>'cuttime','title1'=>'每隔','type'=>'string_two','unit1'=>'分钟','name2'=>'cutprice','title2'=>'价格下降','unit2'=>'元'),
								array('name'=>'starttime','title'=>'开始时间','type'=>'time'),
								array('name'=>'buynum','title'=>'每人最多购买','type'=>'string'),
								array('type'=>'switch','title'=>'仅会员参加','name'=>'only_member','help'=>''),
								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),								
								array('type'=>'text','title'=>'活动描述','name'=>'content','help'=>''),
								
							)
						),
			
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['title'])){
		exi('请设置活动标题','error');
	}
	if(!$postdata['goodsid']){
		exi('请选择商品','error');
	}
	if(empty($postdata['images'])){
		exi('请设置商品图片','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'content'=>trim($postdata['content']),
		'images'=>ijson_encode($postdata['images']),
		'goodsid'=>intval($postdata['goodsid']),
		'total'=>intval($postdata['total']),
		'original'=>intval($postdata['original']),
		'startprice'=>intval($postdata['startprice']),
		'stopprice'=>intval($postdata['stopprice']),
		'cuttime'=>intval($postdata['cuttime']),
		'cutprice'=>intval($postdata['cutprice']),
		'displayorder'=>intval($postdata['displayorder']),
		'only_member'=>intval($postdata['only_member']),
		'status'=>intval($postdata['status']),
		'buynum'=>intval($postdata['buynum']),
		'starttime'=>intval(strtotime($postdata['starttime'])),
	);
	//计算预计结束时间
	$chajia=$postdata['startprice']-$postdata['stopprice'];
	$time=$chajia/$postdata['cutprice']*$postdata['cuttime'];
	$data['endtime']=$time*60+strtotime($postdata['starttime']);
	//print_r(date('Y-m-d H:i',$data['endtime']));exit;
	$id=intval($postdata['id']);
	if($id){
		
		pdo_update('jy_weishop_cutprice_act',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_weishop_cutprice_act',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_cutprice_act',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的活动！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_weishop_cutprice_act',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['only_member'])){
		pdo_update('jy_weishop_cutprice_act',array('only_member'=>intval($post['only_member'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
